<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <cpn></cpn>
</div>

<template id="cpn">
    <div>
        <div>我是子组件</div>
        <button @click="btnClick">我是按钮</button>
        <ccpn></ccpn>
    </div>
</template>

<template id="ccpn">
    <div>
        <div>我是子子组件</div>
        <button @click="btnClick">我是按钮</button>
    </div>
</template>
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: '#app',
        data() {
            return{
                message: 'Hello Vue!'
            }
        },
        components: {
            cpn: {
                template: '#cpn',
                data() {
                    return {
                        name: '我是CPN组件的name'
                    }
                },
                methods: {
                    btnClick() {
                        // 1. 访问父组件
                        console.log(this.$parent);
                    }
                },
                components: {
                    ccpn: {
                        template: '#ccpn',
                        methods: {
                            btnClick() {
                                // 1. 访问父组件
                                console.log(this.$parent);
                                console.log(this.$parent.name);

                                // 可以直接$root访问vue实例
                                console.log(this.$root);
                                console.log(this.$root.message);
                            }
                        }
                    }
                }
            }
        }
    })
</script>

</body>
</html>
